package com.yiren.backstage.db.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.yiren.backstage.currency.entity.record.ProjectGradeEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "PROJECT_GRADE_ENTITY".
*/
public class ProjectGradeEntityDao extends AbstractDao<ProjectGradeEntity, Long> {

    public static final String TABLENAME = "PROJECT_GRADE_ENTITY";

    /**
     * Properties of entity ProjectGradeEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property ProjectId = new Property(1, Long.class, "projectId", false, "PROJECT_ID");
        public final static Property Cause = new Property(2, String.class, "cause", false, "CAUSE");
        public final static Property Grade = new Property(3, int.class, "grade", false, "GRADE");
        public final static Property Time = new Property(4, long.class, "time", false, "TIME");
    }

    private Query<ProjectGradeEntity> projectInfoEntity_ProjectGradeQuery;

    public ProjectGradeEntityDao(DaoConfig config) {
        super(config);
    }
    
    public ProjectGradeEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"PROJECT_GRADE_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"PROJECT_ID\" INTEGER," + // 1: projectId
                "\"CAUSE\" TEXT," + // 2: cause
                "\"GRADE\" INTEGER NOT NULL ," + // 3: grade
                "\"TIME\" INTEGER NOT NULL );"); // 4: time
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"PROJECT_GRADE_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ProjectGradeEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long projectId = entity.getProjectId();
        if (projectId != null) {
            stmt.bindLong(2, projectId);
        }
 
        String cause = entity.getCause();
        if (cause != null) {
            stmt.bindString(3, cause);
        }
        stmt.bindLong(4, entity.getGrade());
        stmt.bindLong(5, entity.getTime());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ProjectGradeEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long projectId = entity.getProjectId();
        if (projectId != null) {
            stmt.bindLong(2, projectId);
        }
 
        String cause = entity.getCause();
        if (cause != null) {
            stmt.bindString(3, cause);
        }
        stmt.bindLong(4, entity.getGrade());
        stmt.bindLong(5, entity.getTime());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ProjectGradeEntity readEntity(Cursor cursor, int offset) {
        ProjectGradeEntity entity = new ProjectGradeEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // projectId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // cause
            cursor.getInt(offset + 3), // grade
            cursor.getLong(offset + 4) // time
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ProjectGradeEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setProjectId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setCause(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setGrade(cursor.getInt(offset + 3));
        entity.setTime(cursor.getLong(offset + 4));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ProjectGradeEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ProjectGradeEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ProjectGradeEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "projectGrade" to-many relationship of ProjectInfoEntity. */
    public List<ProjectGradeEntity> _queryProjectInfoEntity_ProjectGrade(Long projectId) {
        synchronized (this) {
            if (projectInfoEntity_ProjectGradeQuery == null) {
                QueryBuilder<ProjectGradeEntity> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ProjectId.eq(null));
                projectInfoEntity_ProjectGradeQuery = queryBuilder.build();
            }
        }
        Query<ProjectGradeEntity> query = projectInfoEntity_ProjectGradeQuery.forCurrentThread();
        query.setParameter(0, projectId);
        return query.list();
    }

}
